package com.xiaoxing.salesclient.mvp.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jess.arms.base.BaseFragment;
import com.jess.arms.base.BaseLazyFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xiaoxing.gifloadingview.LoadingDialogUtil;
import com.xiaoxing.salesclient.di.component.DaggerJiFenShangChengTabComponent;
import com.xiaoxing.salesclient.di.module.JiFenShangChengTabModule;
import com.xiaoxing.salesclient.mvp.contract.JiFenShangChengTabContract;
import com.xiaoxing.salesclient.mvp.model.entity.OrderExchange;
import com.xiaoxing.salesclient.mvp.model.entity.ProductList;
import com.xiaoxing.salesclient.mvp.presenter.JiFenShangChengTabPresenter;
import com.xiaoxing.salesclient.mvp.ui.adapter.JiFenShangChengTabAdapter;
import com.xiaoxing.salesclient.mvp.ui.entity.JiFenShangChengTab;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import me.jessyan.armscomponent.commonsdk.core.BaseConstants;
import me.jessyan.armscomponent.commonsdk.core.RouterHub;
import me.jessyan.armscomponent.commonsdk.utils.Utils;
import xiaoxing.com.salesclient.R;
import xiaoxing.com.salesclient.R2;

import static android.support.v7.widget.DividerItemDecoration.VERTICAL;
import static com.jess.arms.utils.Preconditions.checkNotNull;
import static com.xiaoxing.salesclient.mvp.ui.fragment.ChanPinJiaGeTabFragment.TYPE_ID;


public class JiFenShangChengTabFragment extends BaseLazyFragment<JiFenShangChengTabPresenter> implements JiFenShangChengTabContract.View, OnRefreshListener, JiFenShangChengTabAdapter.OrderExchange {


    private static boolean mIsNeedDemo = true;
    TextView empty_text;
    ImageView empty_image;

    View mEmptyLayout;
    RecyclerView mRecyclerView;
    RefreshLayout mRefreshLayout;
    private JiFenShangChengTabAdapter mAdapter;
    private String mTypeId = "";
    private List<ProductList.DataBean> mDataBeanList = new ArrayList<>();

    public static JiFenShangChengTabFragment newInstance(String type_id) {
        JiFenShangChengTabFragment fragment = new JiFenShangChengTabFragment();
        Bundle args = new Bundle();
        args.putString(TYPE_ID, type_id);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {
        DaggerJiFenShangChengTabComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .jiFenShangChengTabModule(new JiFenShangChengTabModule(this))
                .build()
                .inject(this);
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_ji_fen_shang_cheng_tab, null);
        getTypeId();

        mRefreshLayout = (RefreshLayout) view.findViewById(R.id.refreshLayout);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        mEmptyLayout = (View) view.findViewById(R.id.empty);
        empty_text = (TextView) view.findViewById(R.id.empty_text);
        empty_image = (ImageView) view.findViewById(R.id.empty_image);

        initRefreshLayout();
        initRecyclerView();
        initEmpty();
        return view;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {


    }

    private void getTypeId() {
        mTypeId = getArguments().getString(TYPE_ID);
    }

    private void initRefreshLayout() {
        mRefreshLayout.setRefreshHeader(new ClassicsHeader(getActivity()).setSpinnerStyle(SpinnerStyle.FixedBehind).setPrimaryColorId(R.color.public_colorPrimary).setAccentColorId(android.R.color.white));
        mRefreshLayout.setOnRefreshListener(this);
        mRefreshLayout.setEnableLoadMore(false);
    }

    private void initRecyclerView() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
//        mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), VERTICAL));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setAdapter(mAdapter = new JiFenShangChengTabAdapter(getActivity(), mDataBeanList, this));

        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                Utils.navigation(getActivity(), RouterHub.SALES_CLIENT_ZUI_XIN_XIAO_XI_XIANG_QING_ACTIVITY);
            }
        });
    }

    private void initEmpty() {
        empty_image.setImageResource(R.drawable.no_data);
        empty_text.setTextColor(getResources().getColor(R.color.public_no_data));
        empty_text.setText("暫無消息,下拉更新");
    }


    /**
     * 通过此方法可以使 Fragment 能够与外界做一些交互和通信, 比如说外部的 Activity 想让自己持有的某个 Fragment 对象执行一些方法,
     * 建议在有多个需要与外界交互的方法时, 统一传 {@link Message}, 通过 what 字段来区分不同的方法, 在 {@link #setData(Object)}
     * 方法中就可以 {@code switch} 做不同的操作, 这样就可以用统一的入口方法做多个不同的操作, 可以起到分发的作用
     * <p>
     * 调用此方法时请注意调用时 Fragment 的生命周期, 如果调用 {@link #setData(Object)} 方法时 {@link Fragment#onCreate(Bundle)} 还没执行
     * 但在 {@link #setData(Object)} 里却调用了 Presenter 的方法, 是会报空的, 因为 Dagger 注入是在 {@link Fragment#onCreate(Bundle)} 方法中执行的
     * 然后才创建的 Presenter, 如果要做一些初始化操作,可以不必让外部调用 {@link #setData(Object)}, 在 {@link #initData(Bundle)} 中初始化就可以了
     * <p>
     * Example usage:
     * <pre>
     * public void setData(@Nullable Object data) {
     *     if (data != null && data instanceof Message) {
     *         switch (((Message) data).what) {
     *             case 0:
     *                 loadData(((Message) data).arg1);
     *                 break;
     *             case 1:
     *                 refreshUI();
     *                 break;
     *             default:
     *                 //do something
     *                 break;
     *         }
     *     }
     * }
     *
     * // call setData(Object):
     * Message data = new Message();
     * data.what = 0;
     * data.arg1 = 1;
     * fragment.setData(data);
     * </pre>
     *
     * @param data 当不需要参数时 {@code data} 可以为 {@code null}
     */
    @Override
    public void setData(@Nullable Object data) {

    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {

        getPointProduuct();
        mRefreshLayout.finishRefresh();

    }

    private void getPointProduuct() {
        mPresenter.pointproduct(mTypeId);
    }

    @Override
    public void showLoading() {

        LoadingDialogUtil.showGifdialog2(getChildFragmentManager(), R.drawable.public_loading);
    }

    @Override
    public void hideLoading() {
        LoadingDialogUtil.dismissDialog();
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {

    }

    @Override
    protected void loadData() {
        mRefreshLayout.autoRefresh();
    }

    @Override
    public void pointproductSuccessed(ProductList productList) {

        if (productList != null && productList.getData() != null && productList.getData().size() > 0) {
            mEmptyLayout.setVisibility(View.GONE);

            mDataBeanList.clear();
            mDataBeanList.addAll(productList.getData());
            mAdapter.notifyDataSetChanged();
        } else {

            mEmptyLayout.setVisibility(View.VISIBLE);

        }
    }

    @Override
    public void orderExchangeSuccessed(OrderExchange orderExchange) {

        if (orderExchange != null && orderExchange.getCode().equals("200")) {
            ArmsUtils.snackbarText("兌換成功");

            getPointProduuct();
        } else {
            ArmsUtils.snackbarText(orderExchange.getMsg());
        }
//        getPointProduuct();
    }

    @Override
    public void orderExchange(String product_id) {
        mPresenter.orderExchange(product_id, mSharedPreferencesHelper.getString(BaseConstants.TOKEN));
    }
}
